package com.gitee.wsl.flow.combine

import com.gitee.wsl.flow.F
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow


//
// amb / ambWith
//

/**
 * This function is an alias to [race] operator.
 *
 * @see race
 */
@ExperimentalCoroutinesApi
fun <T> F.amb(flow1: Flow<T>, flow2: Flow<T>, vararg flows: Flow<T>): Flow<T> = race(
    buildList(capacity = 2 + flows.size) {
        add(flow1)
        add(flow2)
        addAll(flows)
    },
)

/**
 * This function is an alias to [raceWith] operator.
 *
 * @see raceWith
 */
@ExperimentalCoroutinesApi
fun <T> Flow<T>.ambWith(flow: Flow<T>, vararg flows: Flow<T>): Flow<T> = F.race(
    buildList(capacity = 2 + flows.size) {
        add(this@ambWith)
        add(flow)
        addAll(flows)
    },
)

/**
 * This function is an alias to [race] operator.
 *
 * @see race
 */
@ExperimentalCoroutinesApi
fun <T> F.amb(flows: Iterable<Flow<T>>): Flow<T> = race(flows)
